import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-10-30
 * Time: 23:11
 */
class IntCmp implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);
    }
}
public class Test {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(ret == null || k == 0) {
            return ret;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k,new IntCmp());
        for(int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }
        //K*logN
        for(int i = k; i < arr.length; i++) {
            int peekVal = priorityQueue.peek();
            if(peekVal < arr[i]) {
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        //(N-K)logN
        for(int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }
    public static void main(String[] args) {

    }
}
